﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Enterprise.Management.Common;
using System.IO;
using System.Configuration;
using Enterprise.Management.Business;
using Enterprise.Management.Caches;
using Enterprise.Management.Data;
using Enterprise.Management.Gridview;
using Enterprise.Management.DataTemplates;
using Enterprise.Management.FilterAttributes;

namespace Enterprise.Management.Controllers
{
    [CategoryFunction]
    public class InternalController : BaseController
    {
        InternalBLL bllInternal = new InternalBLL();

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(GridviewPage page)
        {
            var gridview = new Gridview.Gridview(page);
            gridview.Header.Add("网点", "DP_Category.Category_Name");
            gridview.Header.Add("文件", "IntInf_FileName");
            gridview.Header.Add(string.Empty);
            gridview.Header.Last.SetProperty("style", "width:80px");
            gridview.Header.Last.RowDataTemplate = new FilePathDataTemplate("_FilePathDataTemplatePartial")
            {
                FileName = "IntInf_FileName",
                FilePath = "IntInf_FilePath"
            };

            DeptPortalsDataContext context = new DeptPortalsDataContext();
            var query = from u in context.DP_Internal_Info
                        select u;
            var data = IntelligentQueryBuilder.Create<DP_Internal_Info>(query, gridview.Page);

            gridview.Fill(data);

            return PartialView("_GridviewPartial", gridview);
        }

        public ActionResult Create()
        {
            foreach (string upload in Request.Files)
            {
                if (!Request.Files[upload].HasFile()) continue;
                string path = FilePathGeneration.generate((int)FunctionType.InternalManagement, BranchCategoryCache.BranchCategoryId.Value);
                string filename = Path.GetFileName(Request.Files[upload].FileName);

                DP_Internal_Info dp_internal_info = new DP_Internal_Info();
                dp_internal_info.IntInf_CtgrID = BranchCategoryCache.BranchCategoryId.Value;
                dp_internal_info.IntInf_FileName = filename;
                dp_internal_info.IntInf_FilePath = "../Resources/" + FilePathGeneration.getRefPath(path) + "/" + filename;
                dp_internal_info.IntInf_Status = (int)StatusType.Valid;
                dp_internal_info.ModifiedBy = HtmlCache.GetUser().User_ID;
                dp_internal_info.ModifiedDate = DateTime.Now;

                if (bllInternal.Add(dp_internal_info))
                {
                    Request.Files[upload].SaveAs(Path.Combine(path, filename));
                    return RedirectToAction("Index");
                }
                else
                {
                }
            }

            return View();
        }

        public FilePathResult GetFileFromDisk()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "Resources/";
            string fileName = "test.txt";
            return File(path + fileName, "text/plain", "test.txt");
        }

        public ActionResult Delete()
        {
            return View();
        }
    }
}
